Python random.seed bug Flash News List | Blockchain.News
Flash News List

List of Flash News about Python random.seed bug

Time Details
2025-12-09
03:40
Python random.seed Sign Bug: seed(5) equals seed(-5) — Critical Risk for AI and Crypto Trading Backtests

According to @karpathy, CPython’s random.seed ignores the sign of integer seeds, so seed(3) and seed(-3) produce identical RNG streams because the implementation takes the absolute value of PyLong arguments (source: twitter.com/karpathy/status/1998236299862659485; source: github.com/python/cpython/blob/main/Modules/_randommodule.c#L321). The Python docs state that if a is an int, it is used directly, and that the core generator is MT19937, but they only guarantee same seed => same sequence and do not promise distinct sequences for different seeds (source: docs.python.org/3/library/random.html). Karpathy reports this caused train=test leakage in his nanochat setup when he used seed sign to separate train/test splits, creating a serious reproducibility and overfitting risk (source: twitter.com/karpathy/status/1998236299862659485). For trading systems and crypto quants using Python for strategy simulation, Monte Carlo VaR, order routing randomness, or ML model evaluation, audit any pipelines that rely on sign-differentiated seeds or assume seed(n) != seed(-n) to avoid biased backtests and invalid performance metrics (source: twitter.com/karpathy/status/1998236299862659485). Actionable mitigations include avoiding negative-vs-positive seed conventions, using string or bytes seeds that are hashed via SHA-512 under version 2 seeding, or explicitly encoding the sign bit as 2*abs(n)+int(n<0) as noted by Karpathy (source: docs.python.org/3/library/random.html; source: twitter.com/karpathy/status/1998236299862659485).

Source